package com.pai4j.act.controller;

import com.pai4j.act.service.ResourceBehaviorFactory;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.domain.vo.request.request.act.ResourceBehaviorRequestVO;
import com.pai4j.domain.vo.response.UserBaseResponseInfoVO;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.remote.act.IResourceBehaviorClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class ResourceBehaviorController implements IResourceBehaviorClient {

    /**
     *   互动操作
     *
     * @author: CYM-pai
     * @date: 2025/07/31 09:25
     **/
    @Override
    @OperationLog(
        module = "互动管理",
        operation = "用户互动",
        description = "用户进行点赞、收藏等互动操作",
        recordParams = true
    )
    public PAIResponseBean<Long> behavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {

        resourceBehaviorRequest.validateRequestParam();

        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        long count = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                        .behavior(resourceBehaviorRequest.getUserId(),
                                    resourceBehaviorRequest.getResourceType(),
                                    resourceBehaviorRequest.getResourceId());
        return PAIResponseBeanUtil.success(count);
    }
    
    /**
     *  取消互动操作
     *
     * @author: CYM-pai
     * @date: 2025/07/31 09:26
     **/
    @Override
    public PAIResponseBean<Long> cancelBehavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        resourceBehaviorRequest.validateRequestParam();
        
        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        long count = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                .cancelBehavior(resourceBehaviorRequest.getUserId(),
                        resourceBehaviorRequest.getResourceType(),
                        resourceBehaviorRequest.getResourceId());
        return PAIResponseBeanUtil.success(count);
    }
    
    /**
     *  判断用户是否已进行互动
     *
     * @author: CYM-pai
     * @date: 2025/07/31 09:26
     **/
    @Override
    public PAIResponseBean<Boolean> isBehavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        resourceBehaviorRequest.validateRequestParam();
        
        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        Boolean result = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                .isBehavior(resourceBehaviorRequest.getUserId(),
                        resourceBehaviorRequest.getResourceType(),
                        resourceBehaviorRequest.getResourceId());
        return PAIResponseBeanUtil.success(result);
    }
    
    /**
     *  统计资源互动数量
     *
     * @author: CYM-pai
     * @date: 2025/07/31 09:34
     **/
    @Override
    public PAIResponseBean<Long> countBehavior(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        
        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        Long result = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                .countBehavior(resourceBehaviorRequest.getResourceType(),
                        resourceBehaviorRequest.getResourceId());
        return PAIResponseBeanUtil.success(result);
    }


    /**
     *  批量统计资源互动数量
     *
     * @author: CYM-pai
     * @date: 2025/07/31 09:34
     **/
    @Override
    public PAIResponseBean<Map<String, UserBaseResponseInfoVO>> resourceBehaviorUsersCache(@RequestBody ResourceBehaviorRequestVO resourceBehaviorRequest) {
        resourceBehaviorRequest.validateRequestParam();

        // 根据具体互动行为类型走工厂模式获取具体的互动实现类，完成behavior操作
        Map<String, UserBaseResponseInfoVO> result = ResourceBehaviorFactory.getService(resourceBehaviorRequest.getBehaviorType())
                .getResourceBehaviorUsersCache(resourceBehaviorRequest.getResourceId(),
                                               resourceBehaviorRequest.getResourceType());
        if (result == null) {
            return PAIResponseBeanUtil.success(null);
        }
        List<UserBaseResponseInfoVO> userBaseResponseInfoVOList = result.values().stream().toList();
        return PAIResponseBeanUtil.success(userBaseResponseInfoVOList);
    }
    
}
